package cn.rkylin.oms.common.jobs.shop;

import cn.rkylin.apollo.common.util.HttpUtils;
import cn.rkylin.oms.common.jobs.AbstractJob;
import cn.rkylin.oms.system.shop.domain.Shop;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 平台订单更新
 *
 * @Author: jipengcheng
 * @Created: 2018-3-12
 */
@Component("orderUpdateJob")
public class OrderUpdateJob extends AbstractJob {

    @Value("${gateway}")
    private String gateway;

    /**
     * 创建订单更新
     *
     * @param entity
     * @param <T>
     * @throws Exception
     */
    @Override
    public <T> void createJob(T entity) throws Exception {
        Shop shop = (Shop) entity;
        // 没有配置店铺作业开关，或者开关==false则退出
        if (StringUtils.isEmpty(createShopJobs) || createShopJobs.equalsIgnoreCase("false")) {
            return;
        }

        if ("唯品会直发".equalsIgnoreCase(shop.getShopType())) {

            postDatas.put("jobDetail.group", shop.getPrjId());
            postDatas.put("jobDetail.name", "shop_" + shop.getShopId() + "_OrderUpdate");
            postDatas.put("className", "com.taobao.ad.easyschedule.job.HttpJob");
            postDatas.put("jobDetail.description", "平台订单更新_" + shop.getShopName());
            postDatas.put("cloneTime", "");
            postDatas.put("triggerType", "Cron");
            postDatas.put("startTimeAsDate", "");
            postDatas.put("stopTimeAsDate", "");
            postDatas.put("cronExpression", "15 0/1 * * * ?");
            postDatas.put("repeatCount", "4");
            postDatas.put("repeatInterval", "16000");
            postDatas.put("priority", "4");
            postDatas.put("action", "1");
            postDatas.put("projectId", shop.getPrjId());
            postDatas.put("projectName", shop.getPrjName());

            // 重复名值对
            repeatNames.add("targetUrl");
            if ("唯品会直发".equalsIgnoreCase(shop.getShopType())) {
                repeatValues.add(gateway + "/microservice-vip/trade/updateVipOrder");
            }

            repeatNames.add("synchronous");
            repeatValues.add("true");

            repeatNames.add("parameter");
            repeatValues.add("");

            repeatNames.add("tokenValidate");
            repeatValues.add("false");

            repeatNames.add("checkResult");
            repeatValues.add("true");

            repeatNames.add("runType");
            repeatValues.add("hash");

            repeatNames.add("completeTime");
            repeatValues.add("60000");

            repeatNames.add("connTimeout");
            repeatValues.add("60000");

            repeatNames.add("retries");
            repeatValues.add("0");

            repeatNames.add("repeatAlarmNum");
            repeatValues.add("0");

            try {
                String addJobResult = HttpUtils.postMethod(addJobURL, postDatas, repeatNames, repeatValues);
                if (addJobResult.equals("error")) {
                    throw new Exception("店铺：" + shop.getShopName() + "创建“订单更新”作业失败。");
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                throw new Exception("店铺：" + shop.getShopName() + "创建“订单更新”作业出现异常。");
            }
        }
    }
}
